package com.example.controller;

import com.example.common.Result;
import com.example.entity.Artwork;
import com.example.entity.Contest;
import com.example.entity.ContestVO;
import com.example.service.ArtworkService;
import com.example.service.ContestService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 竞赛信息表前端操作接口
 **/
@RestController
@RequestMapping("/contest")
public class ContestController {

    @Resource
    private ContestService contestService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Contest contest) {
        contestService.add(contest);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        contestService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        contestService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Contest contest) {
        contestService.updateById(contest);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Contest contest = contestService.selectById(id);
        return Result.success(contest);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Contest contest ) {
        List<Contest> list = contestService.selectAll(contest);
        return Result.success(list);
    }

    @GetMapping("/getName")
    public Result getName() {
        List<ContestVO> names = contestService.getName();
        return Result.success(names);
    }
    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Contest contest,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Contest> page = contestService.selectPage(contest, pageNum, pageSize);
        return Result.success(page);
    }

    @GetMapping("/getPie")
    public Result getPie() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String,Object>> list = new ArrayList<>();
        List<Contest> all = contestService.selectAll(new Contest());
        Map<String, List<Contest>> collect = all.stream().collect(Collectors.groupingBy(Contest::getStatus));

        for(String key : collect.keySet()){
            Map<String,Object> map = new HashMap<>();
            map.put("name",key);
            map.put("value",collect.get(key).size());
            list.add(map);
        }
        resultMap.put("text","竞赛信息统计");
        resultMap.put("subtext","统计维度：竞赛状态");
        resultMap.put("name","状态");
        resultMap.put("data",list);
        return Result.success(resultMap);
    }

    @GetMapping("/getLine")
    public Result getLine() {
        Map<String,Object> resultMap = new HashMap<>();
        Map<String, Integer> monthCountMap = new HashMap<>();
        List<Contest> list = contestService.selectAll(new Contest());
        for (Contest contest : list) {
            String month = contest.getTime().substring(5, 7);
            // 这里可以对时间进行处理，例如打印、存储或其他操作
            if (monthCountMap.containsKey(month)) {
                // 如果月份已在 Map 中，将计数加 1
                monthCountMap.put(month, monthCountMap.get(month) + 1);
            } else {
                // 如果月份不在 Map 中，添加该月份并将计数初始化为 1
                monthCountMap.put(month, 1);
            }
        }
        resultMap.put("text","竞赛数量统计");
        resultMap.put("subtext","统计维度：月份");
        resultMap.put("mon",monthCountMap);
        return Result.success(resultMap);
    }
}